package com.duowei.diandan.preference;

import android.text.TextUtils;
import android.util.Log;

import com.duowei.diandan.app.App;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class Config {
    private static final String TAG = "Config";

    private static final Config INSTANCE = new Config();

    private Map<String, String> mMap = new HashMap<>();
    private File mFile;

    private Config() {
        mFile = new File(App.getApp().getFilesDir(), "settings.json");
        if (!mFile.exists()) {
            Log.d(TAG, "Config: settings.json not found.");
            try {
                if (mFile.createNewFile()) {
                    Log.d(TAG, "Config: settings.json created.");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        loadData();

    }

    public static Config getInstance() {
        return INSTANCE;
    }

    public String getString(String key, String defValue) {
        String s = mMap.get(key);

        if (s != null) {
            return s;
        } else {
            return defValue;
        }
    }

    public void setString(String key, String value) {
        mMap.put(key, value);
        saveData();
    }

    public boolean getBoolean(String key, boolean defValue) {
        String s = mMap.get(key);

        if (!TextUtils.isEmpty(s)) {
            return Boolean.parseBoolean(s);
        } else {
            return defValue;
        }
    }

    public void setBoolean(String key, boolean value) {
        mMap.put(key, String.valueOf(value));
        saveData();
    }

    private void loadData() {
        try {
            FileReader fr = new FileReader(mFile);
            StringBuilder json = new StringBuilder();
            char[] buf = new char[1024];
            while (fr.read(buf) != -1) {
                json.append(buf);
            }
            fr.close();

            Gson gson = new GsonBuilder().setLenient().create();
            Type type = new TypeToken<HashMap<String, String>>() {}.getType();
            Map<String, String> map = gson.fromJson(json.toString(), type);
            mMap.clear();
            if (map != null) {
                mMap.putAll(map);

            }

        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private void saveData() {
        try {
            FileWriter fw = new FileWriter(mFile);
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String json = gson.toJson(mMap);

            fw.write(json);
            fw.close();

        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }
}
